﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Web;
using Xenta.Utils;

namespace Xenta.Web
{
    public abstract class WebAdmin : ExtApplication
    {
        protected override IServiceContext ServiceContext(HttpContext context)
        {
            return new ServiceContext(context);
        }

        protected override bool IsContentRequest(HttpContext context)
        {
            return context.Request.Path.StartsWith("/Content", 
                StringComparison.InvariantCultureIgnoreCase);
        }
    }

    public class ServiceContext : IServiceContext
    {
        #region Fields

        private readonly HttpContext _httpContext;
        private string _infrastructure;

        #endregion

        #region Ctors

        public ServiceContext(HttpContext context)
        {
            if(context == null)
                throw new ArgumentNullException("context");
            _httpContext = context;
        }

        #endregion

        #region Properties

        public HttpContext HttpContext
        {
            get
            {
                return _httpContext;
            }
        }

        public string Infrastructure
        {
            get
            {
                return _infrastructure;
            }
        }

        public IDictionary<String, String> Config
        {
            get
            {
                return null;
            }
        }

        #endregion

        #region Methods

        public Guid Initialize()
        {
            _infrastructure = ConfigurationManager.AppSettings[Par.In("App").Of(this).Name("Infrastructure")];
            if(String.IsNullOrWhiteSpace(_infrastructure))
            {
                var url = HttpContext.Request.Url;
                if(url.IsLoopback)
                    _infrastructure = "localhost";
                else if(url.HostNameType == UriHostNameType.Dns)
                {
                    string hostnamePattern = ConfigurationManager.AppSettings[Par.In("App").Of<Uri>().Name("HostnamePattern")];
                    if(!String.IsNullOrWhiteSpace(hostnamePattern))
                    {
                        var m = Regex.Match(url.Host, hostnamePattern);
                        if(m.Success && m.Groups["domain"].Success)
                            _infrastructure = m.Groups["domain"].Value;
                    }
                }
                if(String.IsNullOrWhiteSpace(_infrastructure))
                    _infrastructure = url.Host;
            }
            return Guid.NewGuid();
        }

        public Guid Authenticate()
        {
            if(HttpContext.User == null ||
                HttpContext.User.Identity == null ||
                !HttpContext.User.Identity.IsAuthenticated)
            {
                return Guid.Empty;
            }
            return HttpContext.User.Identity.Name.ParseAs(Guid.Empty);
        }

        #endregion
    }
}